"""
学生视图层
"""
from db.models import Student, School
from interface import common_interface, student_interface
from lib.utils import prettyprint, loop_input, login_auth

login_user = {'username': ''}


def register():
    """学生注册"""
    while True:
        student_name = input('请输入注册的学生姓名:').strip()
        password = input('请输入密码:').strip()
        age = loop_input('请输入学生年龄:', '年龄必须为0~120的数字！请重新输入！',
                         lambda x: int(x) <= 120 if x.isdigit() else False)
        gender = loop_input('请选择学生性别[1-男][2-女]:', '必须输入数字1[男]或2[女]！请重新输入！',
                            lambda x: x in ['1', '2'])

        student_obj = Student(student_name, password, age, gender)
        success, msg = common_interface.register_interface(student_obj, 2)  # 调用公共注册接口
        print(msg)  # 打印消息
        if success:  # 注册成功
            break


def login():
    """学生登录"""
    username = input('请输入学生姓名:').strip()
    password = input('请输入密码:').strip()

    success, msg = common_interface.login_interface(username, password, 2)  # 调用公共登录接口
    print(msg)  # 打印消息
    if success:  # 登录成功
        login_user['username'] = username


@login_auth(login, login_user)
def select_school():
    """选择学校"""
    # 调用接口打印学校
    school_name_list = common_interface.list_school_interface()
    if not school_name_list:
        print('当前学校列表为空，请联系管理员！')
        return
    # 选择学校
    index = int(loop_input('请输入选择的学校序号:', '输入错误,请重新输入！',
                           lambda x, y: int(x) in range(1, len(y) + 1) if x.isdigit() else False, school_name_list))
    school_name = school_name_list[index - 1]

    # 调用选择学校接口
    success, msg = student_interface.select_school_interface(login_user['username'], school_name)
    print(msg)  # 打印消息


@login_auth(login, login_user)
def select_course():
    """选择课程"""
    # 调用接口打印学校下的课程
    school_name = student_interface.get_school_from_student(login_user['username'])
    if not school_name:  # 未选择学校
        print('请先选择学校！')
        return
    course_name_list = common_interface.list_course_interface(school_name)
    if not course_name_list:  # 学校课程为空
        print('当前学校下没有课程，请联系管理员！')
        return

    # 选择课程
    index = int(loop_input('请输入选择的课程序号:', '输入错误,请重新输入！',
                           lambda x, y: int(x) in range(1, len(y) + 1) if x.isdigit() else False, course_name_list))
    course_name = course_name_list[index - 1]

    # 调用选择课程接口
    success, msg = student_interface.select_course_interface(login_user['username'], course_name)
    print(msg)  # 打印消息


@login_auth(login, login_user)
def check_score():
    """查看分数"""
    # 调用选择课程接口
    success, msg = student_interface.check_score_interface(login_user['username'])
    print(msg)  # 打印消息


student_dict = {
    '1': register,
    '2': login,
    '3': select_school,
    '4': select_course,
    '5': check_score,
    'T': None,
    'Q': None
}


def run():
    while True:
        username = login_user["username"] if login_user["username"] else '未登录'  # 当前登录的用户名
        prettyprint(
            f'当前学生:{username}',
            ['编号', '功能'],
            [['【1】', '学生注册'],
             ['【2】', '学生登录'],
             ['【3】', '选择校区'],
             ['【4】', '选择课程'],
             ['【5】', '查看分数']],
            8)
        order = loop_input('请输入功能编号[Q返回][T登出]:', '输入错误请重新输入！', lambda x, y: x in y, student_dict)

        if order == 'Q':  # 返回主视图
            login_user['username'] = ''  # 将当前用户设置为空
            return

        if order == 'T':  # 退出登录
            if username != '未登录':
                print('无法退出登录，请先登录！')
            else:
                login_user['username'] = ''  # 将当前用户设置为空
            continue

        student_dict[order]()
        input('按回车继续...')
